<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @selection-change="selectionChange"
      @current-change="currentChange">
      <template #menuLeft>
        <ElButton type="primary" icon="el-icon-plus" size="small" @click="doClaimTask()">签收</ElButton>
      </template>
    </FyCurd>
  </BasicContainer>
</template>

<script>
  import { claimTask, getTotoSignTaskList } from '@/api/admin/workitem'
  import { tableOptionTodo } from '@/const/crud/admin/todo'
  import { mapState } from 'vuex'

  export default {
    name: 'TodoSignTask',
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOptionTodo,
        selectList: []
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {}
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        // this.tableOption.selection = true
      },
      // 列表查询
      getList(page, params) {
        if (this.$isBlank(params)) {
          params = {}
        }

        params.processStatus = 'DQS'
        this.tableLoading = true
        getTotoSignTaskList({ entity: params, query: { pageNum: page.currentPage, pageSize: 20 } })
          .then(response => {
            this.tableData = response.data.data.rows
            this.page.total = response.data.data.total
            // setTodoNum
            this.$emit('setTodoSignTaskNum', this.page.total)
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        if (!this.$isBlank(form.dateScope)) {
          form.startTime = form.dateScope[0]
          form.endTime = form.dateScope[1]
        }
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      selectionChange(list) {
        this.selectList = list
      },
      approvalClick(row, type, size) {
        // 这个name配置于 sys_doc_type 中与流程编码相关联：如leave对应名称
        this.$router.push({
          name: row.docName,
          params: {
            taskId: row.taskId,
            procInstId: row.processInstanceId,
            businessKey: row.businessKey,
            activityId: row.taskDefKey,
            activityName: row.name,
            modelKey: row.processDefinitionKey
          }
        })
      },
      doClaimTask() {
        if (this.$isBlank(this.selectList)) {
          this.$message.warning('请选择签收任务信息！')
          return
        }
        const params = {
          claimTaskVoList: [],
          taskId: '-1',
          processInstanceId: '-1',
          message: '签收',
          commentTypeEnum: 'QS',
          processStatusEnum: 'SPZ'
        }
        this.selectList.forEach(row => {
          params.claimTaskVoList.push({
            taskId: row.taskId,
            processInstanceId: row.processInstanceId,
            message: '签收',
            commentTypeEnum: 'QS',
            processStatusEnum: 'SPZ',
            activityId: row.taskDefKey,
            activityName: row.name
          })
        })
        // 签收任务。
        this.$confirm(`是否签收${this.selectList.length}条任务?`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 签收
          claimTask(params).then(response => {
            if (response.data.success) {
              this.$message.success(response.data.msg)
              this.refreshChange()
            }
          })
        })
      }
    }
  }
</script>
